package com.yoomiito.app.ui.my.stock;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.model.bean.OrderInfo;
import com.yoomiito.app.model.bean.OrderList;
import com.yoomiito.app.model.bean.StockBean;
import com.yoomiito.app.model.bean.StockInfo;
import com.yoomiito.app.ui.order.detail.OrderInfoActivity;
import com.yoomiito.app.ui.order.list.OrderActivity;
import com.yoomiito.app.ui.order.list.OrderAdapter;
import com.yoomiito.app.ui.order.list.OrderFmPresenter2;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.widget.dialog.StockDialog;
import com.yoomiito.app.widget.state.ContentType;
import com.yoomiito.app.widget.state.EmptyType;
import com.yoomiito.app.widget.state.LoadingType;
import com.yoomiito.app.widget.state.StatusManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;

public class StockFragment extends BaseLazyFragment<StockFmPresenter> {
    @BindView(R.id.fm_rcy)
    RecyclerView mRecyclerView;
    @BindView(R.id.fm_refreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;

    int currentPage = 1;
    private int mType;
    private BaseQuickAdapter<StockInfo, BaseViewHolder> orderAdapter;
    private String productId;
    private boolean isRefresh;

    private StockDialog dialog;

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public View getLayoutView() {
        mStatusManager = new StatusManager.Builder(context)
                .addType(new ContentType(R.layout.fm_rcy))
                .addType(new LoadingType())
                .addType(new EmptyType().setEmptyContent("无库存"))
                .build();
        return mStatusManager.view();
    }


    @Override
    public StockFmPresenter newP() {
        return new StockFmPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        mType = bundle.getInt(Constant.TYPE);
        productId = bundle.getString(Constant.KEY);
        initRcy();
        initRefreshLayout();
    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        L.i("订单列表收到：" + eventMessage.getEventType() + "   " + eventMessage.getGoodsType());
        if (AllCode.UPDATE_DATA.equals(eventMessage.getEventType())) {//只刷新当前界面数据
            if (eventMessage.getGoodsType() == mType) {
                mSmartRefreshLayout.autoRefresh();
            }
        }
    }

    private void initRefreshLayout() {
        mSmartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            currentPage = 1;
            getP().queryStockList(currentPage,mType, productId);
        });
        mSmartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage++;
            getP().queryStockList(currentPage,mType, productId);
        });
        mSmartRefreshLayout.autoRefresh();
    }

    private void initRcy() {
        mRecyclerView.setPadding(DensityUtil.dp2px(12), DensityUtil.dp2px(10), DensityUtil.dp2px(12), 0);
        LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        orderAdapter = new StockAdapter(null);
        mRecyclerView.setAdapter(orderAdapter);
        orderAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (!TextUtils.isEmpty(orderAdapter.getData().get(position).bizId))
                getP().recommendDetail(orderAdapter.getData().get(position).bizId, orderAdapter.getData().get(position).bizType);
        });
    }

    public void setData(StockBean orderList) {
        isRefresh = true;
//        EventMessage eventMessage = new EventMessage();
//        eventMessage.setCode(orderList.total);
//        eventMessage.setEventType("stock");
//        EventBus.getDefault().post(eventMessage);
        if (currentPage == 1) {
            if (orderList.data == null || orderList.data.size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
                mStatusManager.showEmpty();
            } else {
                mStatusManager.showContent();
                orderAdapter.setNewData(orderList.data);
                if (orderList.total <= orderList.data.size()) {
                    mSmartRefreshLayout.setEnableLoadMore(false);
                }else{
                    mSmartRefreshLayout.setEnableLoadMore(true);
                }
            }
            mSmartRefreshLayout.finishRefresh(true);
        } else {
            if (orderList.data == null || orderList.data.size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                if (orderList.data.size() <= 10) {
                    mSmartRefreshLayout.setEnableLoadMore(false);
                }
                orderAdapter.addData(orderList.data);
            }
            mSmartRefreshLayout.finishLoadMore(true);
        }
        currentPage = orderList.current_page;

    }


    public void setdatas(OrderInfo orderList) {
        if (dialog == null) {
            dialog = new StockDialog(context);
        }
        dialog.setData(orderList);
        dialog.show();

    }

    public void finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true);
        mSmartRefreshLayout.finishLoadMore(true);
    }

}
